export default {
  install(Vue) {
    /**
     *
     * @param {Promise} promise
     * @param {Object} errMsg
     * @returns
     */
    const to = (promise, errMsg) => {
      if (typeof promise !== 'function' && !promise.then)
        throw new TypeError('not a promise')
      return promise
        .then((result) => {
          return [result, undefined]
        })
        .catch((error) => {
          if (errMsg) return [null, { ...errMsg, error }]
          return [null, error]
        })
    }

    Vue.prototype.$to = to
  }
}
